package com.bwie.app.service.impl;

import com.bwie.app.mapper.AppMapper;
import com.bwie.app.service.AppService;
import com.bwie.common.domain.Goods;
import com.bwie.common.domain.Sort;
import com.bwie.common.domain.request.GoodsReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private AppMapper appMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 列表
     * @param req
     * @return
     */
    @Override
    public List<Goods> list(GoodsReq req) {
        return appMapper.list(req);
    }
    /**
     * 回显
     */
    @Override
    public Goods echo(Integer id) {
        return appMapper.echo(id);
    }

    /**
     * 下拉框
     */
    @Override
    public List<Sort> dropDownBox() {
        // 商品分类下拉框需正确展示所有分类信息，数据源来自Redis缓存
        List<Sort> o = (List<Sort>) redisTemplate.opsForValue().get("dropDownBox");
        if (o != null){
            return o;
        }
        // 查询所有
        List<Sort> list = appMapper.dropDownBox();
        // 商品分类数据同步至Redis缓存中
        redisTemplate.opsForValue().set("dropDownBox",list);
        return list;
    }
    /**
     * 购买
     */
    @Override
    public void buy(Integer userId, BigDecimal points) {
        appMapper.buy(userId,points);
    }
}
